<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Publishapi extends Controller
{
	public function publish(Request $request){
		$Globalkey = Db::name('system')->field('publishkey')->find();
		if(!$Globalkey){
			return jsonmsg(404,"未配置安全密钥!");
		}
		$data = $request->param();
		if(empty($data)){
			return jsonmsg(404,"MuYuCMS!");
		}
		if(!password_verify($data['publishkey'],$Globalkey['publishkey'])){
			return jsonmsg(-1,"安全密钥错误!");
		}
		unset($data['publishkey']);
		if(empty($data['mid'])){
			return jsonmsg(-1,"栏目未指定!");
		}
		if(empty($data['modid'])){
			$modid = Db::name('category')->field('modid')->find($data['mid']);
			$data['modid'] = Db::name('model')->field('tablename')->find($modid);
		}else if(is_numeric($data['modid'])){
			$data['modid'] = Db::name('model')->field('tablename')->find($data['modid']);
		}else{
			$m = Db::name('model')->where(['tablename'=>$data['modid']])->field('tablename')->find();
			if($m){
				$data['modid'] = $m;
			}else{
				return jsonmsg(-1,"模型不存在!");
			}
		}
		$tab = $data['modid']['tablename'];
		unset($data['modid']);
	    if(isset($data['editor'])){
	        $data['editor'] = html_entity_decode(htmlspecialchars_decode($data['editor']));
	    }
		if(!isset($data['create_time'])){
			$data['create_time'] = time();
		}else if(!isset($data['update_time'])){
			$data['update_time'] = time();
		}
		$setid = setconid();
		$data['id'] = $setid['id'] + 1;
		unset($setid);
		$ord = Db::name($tab)->select();//当前模型表文章自动排序
		if($ord != NULL || $ord != ""){
		    foreach($ord as $key=>$val){
		        $data['orders'] = $val["orders"] + 1;
		    }
		}
		unset($ord);
		$data['isadmin'] = 1;
		$data['author'] = isset($data['author']) ? $data['author'] : "admin";
		$data['uid'] = isset($data['uid']) ? $data['uid'] : "1";
	    $res = Db::name($tab)->insert($data);
	    if($res){
	        $lms = Db::name("category")->where("id",$data["mid"])->field("pid")->find();
	        if(!empty($lms)){
	            if($lms["pid"] != "0"){
	                Db::name("category")->where(["id"=>$lms["pid"]])->setInc("ar_cont");
	                Db::name("category")->where(["id"=>$data["mid"]])->setInc("ar_cont");
	            }else{
	                Db::name("category")->where("id",$data["mid"])->setInc("ar_cont");
	            }
	        }
			if(Db::name('admin')->find($data['uid'])){
				Db::name("admin_data")->where("uid",$data['uid'])->setInc("contribute");
			}
	        //建立新增文章附属表
	        Db::name($tab."_data")->insert(['aid'=>$data['id']]);
	        //在今日大数据表文章发布数字段自增1
	        Db::name('bigdata')->whereTime('create_time','today')->setInc('article_add');
	        return jsonmsg(1,"添加成功");
	    }else{
	        return jsonmsg(0,"添加失败");
	    }
	}
	
	public function getcategory(Request $request)
	{
		$Globalkey = Db::name('system')->field('publishkey')->find();
		if(!$Globalkey){
			return jsonmsg(404,"未配置安全密钥!");
		}
		$data = $request->param();
		if(empty($data)){
			return jsonmsg(404,"MuYuCMS!");
		}else if(!password_verify($data['publishkey'],$Globalkey['publishkey'])){
			return jsonmsg(-1,"安全密钥错误!");
		}
		$category = Db::name('category')->field('id,title')->select();
		$str = '<br>';
		if(!empty($category)){
			foreach($category as $key=>$v){
				$str .= '<p>'.$v['id'].'<a>'.$v['title'].'</a></p>';
			}
		}
		$str .= '</br>';
		return $str;
	}
}